package com.amigo.online.provider.sync.handle.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.MusicMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicSolrDataProjectProperties;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.handle.data.solr.service.MusicSolrService;
import com.amigo.online.provider.sync.handle.service.AbstractDataHandler;
import com.amigo.online.provider.sync.handle.service.DataHandler;

@Service
public class MusicSolrDataHandler extends AbstractDataHandler implements DataHandler {

    @Autowired
    MusicSolrService musicSolrService;

    @Autowired
    BasicSolrDataProjectProperties basicSolrDataProjectProperties;

    @Autowired
    @Qualifier("solrMusicConvertServiceImpl")
    ConvertJsonObjectService<SolrInputDocument, JSONObject, MusicMysqlInfo> solrMusicconvertJsonObjectService;

    @Override
    public void updateHandle(JSONObject newObject, JSONObject oldObject) {
        SolrInputDocument document = new SolrInputDocument();
        Long id = newObject.getLong("id");
        document.addField("id", id);
        boolean updateFlag = false;
        Map<String, String> projectMap = basicSolrDataProjectProperties.getMusicProjectMap();
        Map<String, String> classMap = basicSolrDataProjectProperties.getVideoClassMap();
        for (String temp : oldObject.keySet()) {
            if (basicSolrDataProjectProperties.getMusicUpdateList().contains(temp)) {
                String solrColumnName = projectMap.get(temp);
                String solrMusicClassName = classMap.get(temp);
                if (solrColumnName != null) {
                    temp = solrColumnName;
                }
                Map<String, Object> partialUpdate = new HashMap<>(1);
                if (solrMusicClassName == null) {
                    partialUpdate.put("set", newObject.get(temp));
                } else {
                    try {
                        Class<?> clazz = Class.forName(solrMusicClassName);
                        partialUpdate.put("set", newObject.getObject(temp, clazz));
                    } catch (ClassNotFoundException e) {
                        logger.info("更新获取video中" + temp + "字段类型失败！");
                    }
                }
                document.addField(temp, partialUpdate);
                updateFlag = true;
            }
        }
        if (updateFlag == true) {
            document.addField("_version_", 1);
            musicSolrService.updateObject(id.toString(), document);
        }
    }

    @Override
    public void insertHandle(JSONObject newObject) {
        SolrInputDocument document = solrMusicconvertJsonObjectService.convert(newObject);
        musicSolrService.insertObject(document);

    }

    @Override
    public void updateInfoByIdThroughHashMap(Long id, Map<String, Object> updateMap) {

    }

    @Override
    public void insertObject(Object source) {
        SolrInputDocument document = solrMusicconvertJsonObjectService.convertBeanToObject((MusicMysqlInfo) source);
        musicSolrService.insertObject(document);
    }

}
